import 'dart:async';

import 'package:connectivity_plus/connectivity_plus.dart';


import 'package:enterprise_v6_app/app/core/utils/log_util.dart';

abstract class NetworkUtil {

  static NetworkType _networkType = NetworkType.unknown;

  static StreamSubscription<ConnectivityResult>? _subscription;


  /// 监听网络情况
  static void init() {
    _subscription ??= Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
      AppLogger.log('网络情况 == $result');
      switch (result) {
        case ConnectivityResult.wifi:
          _networkType = NetworkType.wifi;
          break;
        case ConnectivityResult.mobile:
          _networkType = NetworkType.mobile;
          break;
        case ConnectivityResult.none:
          _networkType = NetworkType.none;
          break;
        default:
          _networkType = NetworkType.unknown;
          break;
      }
    });
  }

  static void dispose() {
    _subscription?.cancel();
    _subscription = null;
  }

  static NetworkType get network => _networkType;
}

enum NetworkType {
  wifi,
  mobile,
  none,
  unknown,
}
